<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Usage</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-memcache.quickstart.configuration.html">Setup</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-memcache.setup.html">安装／配置</a></div>
 <div class="up"><a href="mysqlnd-memcache.quickstart.html">Quickstart and Examples</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqlnd-memcache.quickstart.usage" class="section">
  <h2 class="title">Usage</h2>
  <p class="para">
   After associating a MySQL connection with a Memcache connection using 
   <span class="function"><strong>mysqnd_memcache_set()</strong></span> the plugin attempts to transparently
   replace SQL <em>SELECT</em>
   statements by a memcache access. For that purpose the plugin monitors
   all SQL statements executed and tries to match the statement string
   against <strong><code>MYSQLND_MEMCACHE_DEFAULT_REGEXP</code></strong>.
   In case of a match, the mysqlnd memcache plugin checks whether the
   <em>SELECT</em> is accessing only columns of a mapped table and
   the <em>WHERE</em> clause is limited to a single key lookup.
  </p>
  <p class="para">
   In case of the example SQL table, the plugin will use the Memcache interface
   of the MySQL server to fetch results for a SQL query like
   <em>SELECT f1, f2, f3 WHERE id = n</em>.
  </p>
  <p class="para">
   <div class="example" id="example-1904">
    <p><strong>Example #1 
     Basic example.</strong></p>

    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"host"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"passwd"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$memc&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Memcached</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$memc</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">"host"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysqlnd_memcache_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$memc</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;&nbsp;&nbsp;This&nbsp;is&nbsp;a&nbsp;query&nbsp;which&nbsp;queries&nbsp;table&nbsp;test&nbsp;using&nbsp;id&nbsp;as&nbsp;key&nbsp;in&nbsp;the&nbsp;WHERE&nbsp;part<br />&nbsp;&nbsp;&nbsp;and&nbsp;is&nbsp;accessing&nbsp;fields&nbsp;f1,&nbsp;f2&nbsp;and&nbsp;f3.&nbsp;Therefore,&nbsp;mysqlnd_memcache<br />&nbsp;&nbsp;&nbsp;will&nbsp;intercept&nbsp;it&nbsp;and&nbsp;route&nbsp;it&nbsp;via&nbsp;memcache.<br />*/<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;f1,&nbsp;f2,&nbsp;f3&nbsp;FROM&nbsp;test&nbsp;WHERE&nbsp;id&nbsp;=&nbsp;1"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_row</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;&nbsp;&nbsp;This&nbsp;is&nbsp;a&nbsp;query&nbsp;which&nbsp;queries&nbsp;table&nbsp;test&nbsp;but&nbsp;using&nbsp;f1&nbsp;in&nbsp;the&nbsp;WHERE&nbsp;clause.<br />&nbsp;&nbsp;&nbsp;Therefore,&nbsp;mysqlnd_memcache&nbsp;can't&nbsp;intercept&nbsp;it.&nbsp;This&nbsp;will&nbsp;be&nbsp;executed<br />&nbsp;&nbsp;&nbsp;using&nbsp;the&nbsp;MySQL&nbsp;protocol<br />*/<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;id&nbsp;FROM&nbsp;test&nbsp;WHERE&nbsp;f1&nbsp;=&nbsp;'Lady'"</span><span style="color: #007700">);<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_row</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$row</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(
    [f1] =&gt; Hello
    [f2] =&gt; World
    [f3] =&gt; !
)
array(
    [id] =&gt; 2
)
</pre></div>
    </div>
   </div>
  </p>

 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-memcache.quickstart.configuration.html">Setup</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-memcache.setup.html">安装／配置</a></div>
 <div class="up"><a href="mysqlnd-memcache.quickstart.html">Quickstart and Examples</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
